/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.model.lists;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.zhiqim.kernel.util.Asserts;
import org.zhiqim.kernel.util.Classes;
import org.zhiqim.kernel.util.Validates;

/**
 * 定义在特定场景创建Lst<String>时，无需指定泛型
 *
 * @version v1.0.0 @author zouzhigang 2016-11-15 新建与整理
 */
public abstract class ListS implements Iterable<String>
{
    /***********************************************************/
    //返回List实例的方法
    /***********************************************************/
    
    /**
     * 返回对应的List实例
     * 
     * @return          List实例
     */
    public abstract List<String> instance();
    
    /**
     * 从原来的FListS中放置所有的元素到当前FListS
     * 
     * @param list      原有FListS
     * @return          返回自身
     */
    public ListS addAll(ListS list)
    {
        instance().addAll(list.instance());
        return this;
    }

    public <T> void addAll(List<T> list, String method)
    {
        if (Validates.isEmpty(list))
            return;
        
        try
        {
            Method m = list.get(0).getClass().getMethod(method);
            Asserts.as(m != null?null:"该方法["+method+"]不存在，或不是无参数方法");
            Asserts.as((!Classes.isStaticMethod(m))?null:"该方法["+method+"]是静态方法，不支持");
            
            for (T item : list)
            {
                add((String)m.invoke(item));
            }
        }
        catch(Exception e)
        {
            throw Asserts.exception(e);
        }
    }
    
    /***********************************************************/
    //以为实现List的所有方法
    /***********************************************************/
    
    public int size() 
    {
        return instance().size();
    }

    public boolean isEmpty() 
    {
        return instance().isEmpty();
    }

    public boolean contains(String o) 
    {
        return instance().contains(o);
    }
    
    public Iterator<String> iterator() 
    {
        return instance().iterator();
    }

    public String[] toArray() 
    {
        return instance().toArray(new String[0]);
    }
    
    public boolean add(String e) 
    {
        return instance().add(e);
    }

    public boolean remove(String o) 
    {
        return instance().remove(o);
    }
    
    public boolean containsAll(Collection<String> c)
    {
        return instance().containsAll(c);
    }

    public boolean addAll(Collection<String> c) 
    {
        return instance().addAll(c);
    }

    public boolean addAll(int index, Collection<String> c) 
    {
        return instance().addAll(index, c);
    }
    
    public boolean removeAll(Collection<String> c) 
    {
        return instance().removeAll(c);
    }

    public boolean retainAll(Collection<String> c) 
    {
        return instance().retainAll(c);
    }
    
    public void clear() 
    {
        instance().clear();
    }
    
    public String get(int index) 
    {
        return instance().get(index);
    }

    public String set(int index, String element) 
    {
        return instance().set(index, element);
    }
    
    public void add(int index, String element) 
    {
        instance().add(index, element);
    }

    public String remove(int index) 
    {
        return instance().remove(index);
    }

    public int indexOf(String o) 
    {
        return instance().indexOf(o);
    }

    public int lastIndexOf(String o) 
    {
        return instance().lastIndexOf(o);
    }

    public ListIterator<String> listIterator() 
    {
        return instance().listIterator();
    }

    public ListIterator<String> listIterator(int index) 
    {
        return instance().listIterator(index);
    }

    public List<String> subList(int fromIndex, int toIndex) 
    {
        return instance().subList(fromIndex, toIndex);
    }
    
    public String toString()
    {
        return instance().toString();
    }
}
